package com.wode.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.hydra.editor.main.RichEditorPreview;
import com.wode.ui.R;
import android.app.Activity;
import android.widget.Toast;

import org.jsoup.safety.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class PreviewActivity extends Activity {
     
     private static final String cssStart ="<html>" + "\n"+"<header>" + "\n"+"<style type=\"text/css\"> img {" +"width:90%;" +"height:auto;" +"display:block;" +"clear:both;" +"margin:auto" +"}" + "\n"+"body {" +"margin-right:15px;" +"margin-left:15px;" +"margin-top:15px;" +"font-size:21px;" +"}" + "\n" +"</style>" + "\n" +"</header>" + "\n" +"<body>" +"\n";
     private static final String cssEnd = "\n"+"</body>"+"\n"+"</html>";
    /**预览控件*/
    private RichEditorPreview richEditorPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        initView();
        initClickListener();
    }

    /**
     * 初始化View
     */
    private void initView() {
        Bundle params = getIntent().getExtras();
        String html = params.getString("html");
        richEditorPreview = findViewById(R.id.re_editor_preview);
       if(html.equals("")){
        richEditorPreview.setPlaceholder(getNewContent("暂无预览内容"));
        }else{
        richEditorPreview.setHtmlContent(getNewContent(html));
        }
        
        
      Toast.makeText(getApplicationContext(),html,Toast.LENGTH_SHORT).show();

        //图片预览事件监听
        richEditorPreview.setOnImagePreviewListener((images, currentImgIndex)->{
            if(!images.isEmpty()){
            
                Log.d("RichEditor", "当前点击的图片地址："+images.get(currentImgIndex));
            }
        });

    }
    private String getNewContent(String htmltext) {

        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "90%").attr("height",
                    "auto");
        }
        return doc.toString();
    }
    private void initClickListener() {
    }

}